1792C - Min Max Sort - CodeForces Solution


binary search brute force greedy math math math math math

Please click on ads to support us..

C++ Code:

/*
	author: akhilendra11
*/


#include<bits/stdc++.h>
using namespace std;

#define int long long int
#define endl "\n"

void solve(){
	int n; cin>>n;
	int idx[n+1];

	for(int i=1;i<=n;i++){
		int x; cin>>x;
		idx[x]=i;
	}

	int c=n/2;

	int i=idx[c]; 
	int j=idx[n-c+1];

	if(n==1){
		cout<<"0\n";
		return;
	}
	if(i>j and n%2==0){
		cout<<n/2<<"\n";
		return;
	}

	if(n%2){
		i=j=idx[c+1];
	}
	else c--;
	
	while(c>0 and idx[c]<i and idx[n-c+1]>j){
		i=idx[c];
		j=idx[n-c+1];
		c--;
	}

	cout<<c<<"\n";

}

int32_t main(){

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);

	int t; cin>>t;
	while(t--){
		solve();
	}
	
	return 0;
}


Comments

Submit
0 Comments
More Questions

279A - Point on Spiral
1294D - MEX maximizing
447A - DZY Loves Hash
23B - Party
63D - Dividing Island
1203E - Boxers
1547F - Array Stabilization (GCD version)
358A - Dima and Continuous Line
1385C - Make It Good
651A - Joysticks
1474D - Cleaning
1588A - Two Arrays
816A - Karen and Morning
9D - How many trees
918B - Radio Station
15A - Cottage Village
1737B - Ela's Fitness and the Luxury Number
1425H - Huge Boxes of Animal Toys
1737A - Ela Sorting Books
768C - Jon Snow and his Favourite Number
1006C - Three Parts of the Array
81A - Plug-in
276C - Little Girl and Maximum Sum
1738D - Permutation Addicts
1348B - Phoenix and Beauty
186A - Comparing Strings
1281A - Suffix Three
1421C - Palindromifier
1443A - Kids Seating
963A - Alternating Sum